function noNull(msg) {
  return { required: true, message: msg, trigger: 'blur' }
}

function customLen(rule, value, callback) {
  const len = value.length
  if (len <= 10 && len >= 4) {
    if (/^\w{4,10}$/.test(value)) {
      callback()
    } else {
      callback(new Error('只允许的字符a-zA-Z0-9_'))
    }
    callback()
  } else {
    callback('只允许长度4-10')
  }
}

function phone(rule, value, callback) {
  console.log(value)
  const pattern = /^1[3|4|5|8][0-9]\d{8}$/
  if (pattern.test(value)) {
    callback()
  } else {
    callback(new Error('请输入正确手机号码'))
  }
}

export const emailRule = [
  noNull('请输入邮箱地址'),
  { type: 'email', message: '请输入正确邮箱', trigger: ['blur'] }
]

export const phoneRule = [
  noNull('请输入手机号码'),
  { validator: phone, trigger: ['blur'] }
]

export const usernameRule = [
  noNull('请输入用户名'),
  { validator: customLen, trigger: ['blur'] }
]

export const passwordRule = [
  noNull('请输入密码'),
  { validator: customLen, trigger: ['blur'] }
]
